package com.think.teaching.controller;


import com.think.common.controller.BaseController;
import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.ArticleType;
import com.think.teaching.service.impl.ArticleTypeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 资源类型 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Controller
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "文章类型管理接口")
public class ArticleTypeController extends BaseController{
	
	private final ArticleTypeServiceImpl ArticleTypeService;

	public ArticleTypeController(ArticleTypeServiceImpl ArticleTypeService) {
		this.ArticleTypeService = ArticleTypeService;
	}

	@ApiOperation("添加新的文章类型")
	@PostMapping("admin/articleType")
	public Result AddNewArticleType(@RequestBody()ArticleType data) {
		Result result = data.CheckObjectComplete();
		if(result.getCode() == ResultCode.SUCCESS.code()) {
			ArticleType ArticleType = ArticleTypeService.InsetArticleType(data);
			if( ArticleType!= null) {
				return Result.success(ArticleType);
			}
			else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
		else
			return result;
	}
	
	@ApiOperation("更新文章类型")
	@RequestMapping(value = "admin/articleType",method = RequestMethod.PUT)
	public Result UpdateArticleType(@RequestBody()ArticleType data) {
		Result result = data.CheckObjectComplete();
		if(result.getCode() == ResultCode.SUCCESS.code()) {
			ArticleType articleType = ArticleTypeService.UserSelectArticleType(data.getId(), false);
			if (articleType == null) {
				return Result.failure(ResultCode.ARTICLE_TYPE_NOT_DEFINE);
			}
			articleType = ArticleTypeService.SelectArticleTypeWithName(data.getName(), false);
			if (articleType != null && !articleType.getId().equals(data.getId())) {
				return Result.failure(ResultCode.ARTICLE_TYPE_ALREADY_EXIST);
			}
			articleType = ArticleTypeService.UpdateArticleType(data);
			if( articleType!= null) {
				return Result.success(articleType);
			}
			else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
		else
			return result;
	}
	
	@ApiOperation("删除文章类型")
	@RequestMapping(value = "admin/articleType/{id}",method = RequestMethod.DELETE)
	public Result DeleteArticleType(@PathVariable("id")Integer id) {
		ArticleType ArticleType = ArticleTypeService.UserSelectArticleType(id, false);
		if( ArticleType== null) {
			return Result.failure(ResultCode.ARTICLE_TYPE_NOT_DEFINE);
		}
		else {
			if (ArticleTypeService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	
	@ApiOperation("获取文章类型列表")
	@RequestMapping(value = "admin/articleTypes/{page}/{count}",method = RequestMethod.GET)
	public Result DeleteArticleType(@PathVariable("page")Integer page,@PathVariable("count")Integer count) {
		return Result.success(ArticleTypeService.ListArticleType(page, count, false));

	}
	
	@ApiOperation("获取文章类型详情")
	@RequestMapping(value = "/articleType/{id}",method = RequestMethod.GET)
	public Result GetArticleType(@PathVariable("id")Integer id) {
		ArticleType ArticleType = ArticleTypeService.UserSelectArticleType(id, true);
		if( ArticleType== null) {
			return Result.failure(ResultCode.ARTICLE_TYPE_NOT_DEFINE);
		}
		else {
			
			return Result.success(ArticleType);
		}
	}
	
	@ApiOperation("管理员搜索文章类型")
	@RequestMapping(value = "admin/articleType/search", method = RequestMethod.GET)
	public Result AdminSearchArticleType(@RequestParam("key") String key) {
		return Result.success(ArticleTypeService.ListArticleTypeSearch(key, false));
	}
	
	@ApiOperation("管理员修改文章类型状态")
	@RequestMapping(value = "admin/articleType/{id}/{state}", method = RequestMethod.PUT)
	public Result UpdateArticleState(@PathVariable("id") Integer id, @PathVariable("state") Integer state) {
		ArticleType article = ArticleTypeService.UserSelectArticleType(id, false);
		if (article == null) {
			return Result.failure(ResultCode.ARTICLE_TYPE_NOT_DEFINE);
		} else {
			try {
				article = ArticleTypeService.SetArticleState(id, state);
				return Result.success(article);
			} catch (Exception e) {
				// TODO: handle exception
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
	
}

